package com.atguigu.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.gulimail.product.service.CategoryService;
import com.atguigu.gulimail.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimail.product.vo.AttrGroupWithAttrsVo;
import com.atguigu.gulimail.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.management.relation.RelationService;


/**
 * 属性分组
 *
 * @author huangjianyun
 * @email 2409441649@qq.com
 * @date 2021-09-06 13:57:11
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;


    ///product/attrgroup/{attrgroupId}/attr/relation:
    //自动注入attrService接口
    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;

    //新增分组与属性分类关联
    //  /product/attrgroup/attr/relation 接口
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody  List<AttrGroupRelationVo> vos){
        //前端传递过来数据 存储分组id 商品分类id,进行批量的保存新增的属性分组和属性分类的 添加的属性分组和基本属性的关联关系
        relationService.saveBatch(vos);
        return R.ok();

    }

    //获取属性分组下的所有的属性分组和所有属性
    //  /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        //创建一个vo对象，该对象返回前端想要的数据  所有属性分组 和所有属性
        //三级分类id查询该分类下的所有属性分组和所有属性
        List<AttrGroupWithAttrsVo> groupWithAttrsVoList = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",groupWithAttrsVoList);
    }



    ///product/attrgroup/attr/relation/delete
    //删除属性分组和属性之间的关联关系
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody  AttrGroupRelationVo[]  vos){
    attrService.deleteRelation(vos);
    return R.ok();
    }


    ///product/attrgroup/{attrgroupId}/noattr/relation
    //查询未关联的属性分组的所有基本属性
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam  Map<String,Object>  params){
        //attrService接口根据分组id  分页的参数信息，查询未关联的基本属性
        //这个是分页的查询的方法 这个方法传递分页数据信息，和那个基本属性分组下分组的信息
        //传递分组id
        PageUtils page =  attrService.getNoRelationAttr(params,attrgroupId);
        return  R.ok().put("page",page);

    }


    //根据属性分组id进行获取属性分组的关联的所有基本属性
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId ){
        //attrService接口根据分组id 获取属性实体类的容器
        List<AttrEntity>  entities =  attrService.getRelationAttr(attrgroupId);
        return  R.ok().put("data",entities);

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils  page= attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }
///product/attrgroup/list/{catelogId}

    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils  page= attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        //根据属性分组id获取属性分组记录
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		//查询到catelogPath完整路径返回前端
        //根据商品分类id获取商品分类CatelogPath路径
        Long  catelogId = attrGroup.getCatelogId();
        //[/父/子/孙] 路径  一级/二级/三级 商品分类名称组成
        Long[]  path = categoryService.findCatelogPath(catelogId);
        //路径设置到属性分组的attrGroup名称中
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroupEntity){
		attrGroupService.save(attrGroupEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
